#include "StdAfx.h"
#include "mapNode.h"
#include "Map.h"

//MapNode::MapNode(MAP_STYLE mapStl, CSceneManager* smgr, int i,int j)
//{
//    state = st;
//    mMnode = NULL;
//    m_i = i;
//    m_j = j;
//    render(smgr);
//}
//
//MapNode::~MapNode(void)
//{
//}
//
//void MapNode::render(CSceneManager *smgr)
//{
//    if(m_i < 10 && m_j < 10)
//    {
//        float x = m_j*30;
//        float y = m_i*30;
//        mMnode = smgr->addMeshSceneNode("tris.md2",smgr->getRootNode(),Vector3f(x,y,0.0f),Vector3f(0.0f,0.0f,0.0f)
//	            ,Vector3f(1.0f,1.0f,1.0f),"knight_white.jpg");
//        mMnode->setScale(Vector3f(0.05f,0.05f,0.05f));
//    }
//
//}


void MapNode::creatMap(CSceneManager *smgr, char *mapPath)
{
	//smgr->addMeshSceneNode("box.MD2",smgr->getRootNode(),Vector3f(335.0f,0.0f,300.0f)
	//	,Vector3f(0.0f,0.0f,0.0f),Vector3f(1900.0f,0.1f,1500.0f),"ground.jpg");
	Map mapLoader;
	mapLoader.initMap(mapPath);
	std::cout<<"Map Creating..."<<std::endl;
	int node_i=0;
	int node_j=0;
	Vector3f position;
	int nodeState;
	while (node_i<LENTH)
	{
		while (node_j<HEIGHT)
		{
			position = mapLoader.getPos(node_i,node_j);
			nodeState = mapLoader.getNodeState(node_i,node_j);
			creatSceneNode(smgr,nodeState,position,node_i,node_j);
			node_j++;
		}
		node_j = 0;
		node_i++;
	}
}


void MapNode::creatSceneNode( CSceneManager *smgr , int nodeState, Vector3f position, int i, int j)
{
	switch (nodeState)
	{
	case ROAD:
		break;
	case BRICK:
		 sceneNodePtr[i][j] = smgr->addMeshSceneNode("box.MD2",smgr->getRootNode(),position
			,Vector3f(0.0f,0.0f,0.0f),Vector3f(.25f,.25f,.25f),"brick.jpg");
		//std::cout<<x->getAABBBox().maxX<<"   "<<x->getAABBBox().minX<<"   "<<std::endl;
		break;
	case WATER:
		break;
	case TREE:
		break;
	case STONE:
		sceneNodePtr[i][j] = smgr->addMeshSceneNode("box.MD2",smgr->getRootNode(),position
			,Vector3f(0.0f,0.0f,0.0f),Vector3f(.25f,.25f,.25f),"stone.jpg");
		break;
	case HOME:
		sceneNodePtr[i][j] = smgr->addMeshSceneNode("box.MD2",smgr->getRootNode(),position
			,Vector3f(0.0f,0.0f,0.0f),Vector3f(.25f,.25f,.25f),"home.jpg");
		break;
	default:
		std::cout<<"Error map style!"<<std::endl;
	}
}

MapNode::MapNode()
{
	int i=0,j=0;
	while(i<LENTH)
	{
		while (j<HEIGHT)
		{
			sceneNodePtr[i][j] = NULL;
			++j;
		}
		j=0;
		++i;
	}
}

CMeshSceneNode *MapNode::getScenceNodePtr( int i, int j )
{
	return sceneNodePtr[i][j];
}